﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
#include "widget.h"
#include "ui_widget.h"

#include <QFileDialog>
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 创建Base64Worker实例
    base64Worker = new Base64Worker(this);

    ui->pushButtonStart->setEnabled(false);
    // 连接信号槽
    connect(ui->pushButtonSelectFile, &QPushButton::clicked, this, &Widget::onSelectFileClicked);
    connect(ui->pushButtonSaveAs, &QPushButton::clicked, this, &Widget::onSaveAsClicked);
    connect(ui->pushButtonStart, &QPushButton::clicked, this, &Widget::onStartClicked);
    connect(base64Worker, &Base64Worker::progressChanged, this, &Widget::updateProgress);
    connect(base64Worker, &Base64Worker::finished, this, &Widget::onWorkerFinished);
    connect(base64Worker, &Base64Worker::errorOccurred, this, &Widget::onErrorOccurred);
}

Widget::~Widget()
{
    delete ui;
    delete base64Worker;
}

void Widget::onSelectFileClicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("选择文件"), "", tr("所有文件 (*)"));
    if (!fileName.isEmpty())
    {
        // 记录文件总大小
        QFile file(fileName);
        if (file.open(QIODevice::ReadOnly))
        {
            m_totalFileSize = file.size();
            file.close();
            ui->progressBar->setValue(0);
            m_inputFileName = fileName;
            ui->inputFileName->setText(m_inputFileName);
            // 自动生成输出文件名
            m_outputFileName = fileName + ".txt";
            ui->outputFileName->setText(m_outputFileName);
            // 启用启动按钮
            ui->pushButtonStart->setEnabled(true);
        }
        else
        {
            QMessageBox::warning(this, tr("警告"), tr("无法打开文件！"));
        }
    }
}

void Widget::onSaveAsClicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("保存文件"), "", tr("文本文件 (*.txt)"));
    if (!fileName.isEmpty())
    {
        m_outputFileName = fileName;
        ui->outputFileName->setText(m_outputFileName);
    }
}

void Widget::onStartClicked()
{
    // 创建输出文件
    m_outputFile = new QFile(m_outputFileName);
    if (m_outputFile->open(QIODevice::WriteOnly | QIODevice::Text))
    {
        // 设置Base64Worker参数
        base64Worker->setFileName(m_inputFileName);
        base64Worker->setTotalSize(m_totalFileSize);
        base64Worker->setOutputFile(m_outputFile);
        base64Worker->start();
        // 禁用启动按钮
        ui->pushButtonStart->setEnabled(false);
    }
    else
    {
        QMessageBox::warning(this, tr("警告"), tr("无法创建输出文件！"));
    }
}

void Widget::updateProgress(qint64 processedSize)
{
    // 更新进度条
    int progress = static_cast<int>((processedSize * 100.0) / m_totalFileSize);
    ui->progressBar->setValue(progress);
}

void Widget::onWorkerFinished()
{
    // 工作完成后的处理
    QMessageBox::information(this, tr("完成"), tr("Base64编码完成！"));
    m_outputFile->deleteLater();
    // 重新启用启动按钮
    ui->pushButtonStart->setEnabled(true);
}

void Widget::onErrorOccurred(const QString &errorMessage)
{
    // 显示错误信息
    QMessageBox::critical(this, tr("错误"), errorMessage);
    // 清理资源
    if (m_outputFile && m_outputFile->isOpen()) {
        m_outputFile->close();
        m_outputFile->deleteLater();
    }
    // 重新启用启动按钮
    ui->pushButtonStart->setEnabled(true);
}
